package yyds.sniarbtej.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;

public class DebugUtil {

    public static boolean DEBUG = true;

    public static void dump(final String className, final byte[] classfileBuffer) {
        if (DEBUG) {
            String base = System.getProperty("java.io.tmpdir");
            String newName = className.replaceAll("/", "_") + ".class";

            File directory = Paths.get(base, "class").toFile();
            directory.mkdirs();
            final File file = new File(directory, newName);

            System.out.println("[DEBUG] file: " + file);
            try (FileOutputStream writer = new FileOutputStream(file)) {
                writer.write(classfileBuffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void log(final String msg) {
        if (DEBUG) {
            System.out.println(msg);
        }
    }
}
